﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admintractor_Category : System.Web.UI.Page
{
    CategoryBusiness cate = new CategoryBusiness();
    private int id = Helper.getQuery("id");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindGV();
            if (id > 0)
            {
                but_add.Text = "Sửa";
                CategoriesEntities c = cate.getItem(id);
                txtName.Text = c._name;
                txtPriority.Text = c._priority.ToString();
                if (c._display.Trim().Equals("True") || c._display.Trim().Equals("1"))
                {
                    cbView.Checked = false;
                }
                else { cbView.Checked = true; }
            }
            else
            {
                but_add.Text = "Thêm mới";
            }
        }
    }

    public void bindGV()
    {
        GridView1.DataSource = cate.getCategory();
        GridView1.DataBind();
    }

    public string view(string _display)
    {
        string tmp = "";
        if (_display.Equals("1") || _display.Equals("True"))
        {
            tmp = "Có";
        }
        else
        {
            tmp = "Không";
        }
                
        return tmp;
    }
    protected void lbEdit_Click(object sender, EventArgs e)
    {
        Response.Redirect("Category.aspx?id=" + ((LinkButton)sender).CommandArgument);
    }
    protected void lbDelete_Click(object sender, EventArgs e)
    {
        if (cate.delete(int.Parse(((LinkButton)sender).CommandArgument)))
        {
            Response.Redirect("Category.aspx");
        }
        else
        {
            Label1.Text = "No delete";
        }
    }
    private bool check()
    {
        bool flat = true;
        try
        {
            if (txtName.Text.Trim().Equals("") || txtPriority.Text.Trim().Equals("") || int.Parse(txtPriority.Text.Trim()) < 0)
            {
                flat = false;
            }
        }
        catch (FormatException ex)
        {
            flat = false;
        }
        return flat;
    }
    protected void but_add_Click(object sender, EventArgs e)
    {
        if (this.check())
        {
            if (id > 0)
            {
                CategoriesEntities item = new CategoriesEntities();
                item._id = id;
                item._name = txtName.Text.Trim();
                item._priority = int.Parse(txtPriority.Text.Trim());
                item._display = cbView.Checked ? "0" : "1";
                if (cate.edit(item))
                { bindGV(); txtName.Text = ""; txtPriority.Text = ""; cbView.Checked = false; Label1.Text = ""; }
                else { Label1.Text = "Xẩy ra nỗi trong quá trình thực hiện"; }
            }
            else
            {
                CategoriesEntities item = new CategoriesEntities();
                item._name = txtName.Text.Trim();
                item._priority = int.Parse(txtPriority.Text.Trim());
                item._display = cbView.Checked ? "0" : "1";
                if (cate.add(item))
                { bindGV(); txtName.Text = ""; txtPriority.Text = ""; cbView.Checked = false; Label1.Text = ""; }
                else { Label1.Text = "Xẩy ra nỗi trong quá trình thực hiện"; }
            }
        }
        else
        {
            Label1.Text = "Thông tin cần nhập chính sác";
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.bindGV();
    }
}
